//运行结果：
// ConcreteComponent operation
// ConcreteComponent operation
// Added behavior from ConcreteDecoratorA
// ConcreteComponent operation
// Added behavior from ConcreteDecoratorA
// Added behavior from ConcreteDecoratorB

public interface Component {
    void sampleOperation();
}
public class ConcreteComponent implements Component {
    @Override
    public void sampleOperation() {
        System.out.println("ConcreteComponent operation");
    }
}
public abstract class Decorator implements Component {
    protected Component component;

    public Decorator(Component component) {
        this.component = component;
    }

    @Override
    public void sampleOperation() {
        if (component != null) {
            component.sampleOperation();
        }
    }
}
public class ConcreteDecoratorA extends Decorator {
    public ConcreteDecoratorA(Component component) {
        super(component);
    }

    @Override
    public void sampleOperation() {
        super.sampleOperation();
        add额外行为A();
    }

    private void add额外行为A() {
        System.out.println("Added behavior from ConcreteDecoratorA");
    }
}
public class ConcreteDecoratorB extends Decorator {
    public ConcreteDecoratorB(Component component) {
        super(component);
    }

    @Override
    public void sampleOperation() {
        super.sampleOperation();
        add额外行为B();
    }

    private void add额外行为B() {
        System.out.println("Added behavior from ConcreteDecoratorB");
    }
}
public class Client {
    public static void main(String[] args) {
        Component component = new ConcreteComponent();
        Component decoratorA = new ConcreteDecoratorA(component);
        Component decoratorB = new ConcreteDecoratorB(decoratorA);

        component.sampleOperation();
        decoratorA.sampleOperation();
        decoratorB.sampleOperation();
    }
}